package mindmap.components.node;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.QuadCurve;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import mindmap.controller.layoutController;
import mindmap.operation.redoundo;


import java.util.LinkedList;

import static mindmap.controller.layoutController.*;
import static mindmap.controller.layoutController.selectRect;

public class node extends AnchorPane {


    /**
     * 当双边布局时使用调用.
     */
    public static node leftMaxHeight;
    public static node leftMinHeight;
    public static node rightMinHeight;
    public static node rightMaxHeight;
    //给root节点存储左边2级的节点
    public static LinkedList<node> leftChildrenNode = new LinkedList<>();
    //给root节点存储右边2级的节点
    public static LinkedList<node> rightChildrenNode = new LinkedList<>();
    //给root节点计录2级节点右多少个
    public static int allTowNode = 0;

    //用于记录删除节点是要上升的高度
    private double distance;

    //用于记录当前鼠标的位置
    private double x;
    private double y;

    private double xlayout;
    private double ylayout;

    //子节点中高度最高的节点
    public node maxHeightNode;
    //子节点中高度最低的节点
    public node minHeightNode;

    //前一个节点(同级)
    public node PreNode = null;
    //后一个节点(同级)
    public node SufNode = null;

    //要删除的节点
    public static LinkedList<node> deleteList = new LinkedList<>();

    //根节点
    public node root;

    //用于判断当前节点是什么布局
    public String arrangement;


    /**
     * 文本输入框
     */
    protected TextArea textArea = new TextArea();

    /**
     * 字体
     */


    /**
     * 颜色
     */


    /**
     * 节点层级
     */
    private int level = 1;

    /**
     * 子节点
     */
    private LinkedList<node> childrenNode = new LinkedList<>();

    /**
     * 父节点
     */
    private node parentNode;


    /**
     * 用于显示节点文本
     */
    public Label label = new Label();

    private boolean isEdit = false;
    private boolean isBold = false;
    private boolean isUnderline = false;
    private Rectangle rectangle = new Rectangle();
    //线
    private QuadCurve line;


    //张世杰 定义矩形颜色
    private Color rectangleColor = Color.ORANGE;
    private Color borderColor = Color.BLACK;


    public Color getRectangleColor() {
        return rectangleColor;
    }

    public Color getBorderColor() {
        return borderColor;
    }

    ContextMenu menu = new ContextMenu();
    Menu add = new Menu("添加");
    MenuItem children = new MenuItem("孩子");
    MenuItem bro = new MenuItem("兄弟");
    MenuItem delete = new MenuItem("删除");

    public TreeItem<String> getTreeItem() {
        return treeItem;
    }

    public void setTreeItem(TreeItem<String> treeItem) {
        this.treeItem = treeItem;
    }

    //用于展示节点结构
    private TreeItem<String> treeItem = new TreeItem<>();


    /**
     * 用于初始化矩形
     */
    public void initRectangle() {
        rectangle.setLayoutX(0);
        rectangle.setLayoutY(0);
        rectangle.setFill(new Color(255 / 255.0, 187 / 255.0, 0, 1));
        rectangle.setArcHeight(30);
        rectangle.setArcWidth(30);
        rectangle.setWidth(100);
        rectangle.setHeight(40);
        rectangle.setStroke(borderColor);
    }

    Group pane1;

    Group pane2;

    public node(node root, node parentNode, Group pane1, Group pane2, String arrangement) {

        this.arrangement = arrangement;
        this.parentNode = parentNode;
        this.pane1 = pane1;
        this.pane2 = pane2;
        this.root = root;
        this.getChildren().add(rectangle);
        this.getChildren().add(textArea);
        this.getChildren().add(label);
        pane1.getChildren().add(this);

        this.setPrefWidth(100);
        this.setPrefHeight(40);

        //初始化该节点的最高高度和最低高度
        maxHeightNode = null;
        minHeightNode = null;

        //label文本和文本框中的文本进行双向绑定,使的文本框中的内容改变时label中的文本也改变
        //节点中显示的文本是label中的文本
        textArea.textProperty().bindBidirectional(label.textProperty());



        //设置内边距
        label.setPadding(new Insets(10, 10, 10, 10));

        this.initRectangle();
        //this.initLine();

        this.label.setAlignment(Pos.CENTER);
        label.setTextFill(Color.WHITE);
        label.setTextAlignment(TextAlignment.CENTER);
        label.setPrefHeight(this.getPrefHeight());
        label.setPrefWidth(this.getPrefWidth());
        label.setWrapText(true);
        label.setFont(Font.font("Arial"));

        textArea.setPrefHeight(this.getPrefHeight());
        textArea.setPrefWidth(this.getPrefWidth());
        treeItem.valueProperty().bindBidirectional(textArea.textProperty());
        treeItem.setExpanded(true);
        //与节点绑定
        //sizeBind();

        //隐藏文本框
        hideTextArea();

        add.getItems().addAll(children, bro);
        menu.getItems().addAll(add, delete);
        label.setContextMenu(menu);

        initLine();
        bro.setOnAction(event -> {
            if(parentNode == null){
                return;
            }else {
                if(root.arrangement.equals("bilateral")){
                    parentNode.addBothNode(null);
                }else {
                    parentNode.addChrNode(null);
                }
            }

            selectRect.setLayoutY(this.getLayoutY()-3);
        });

        children.setOnAction(event -> {
            mouse();
        });

        delete.setOnAction(event -> {
            if (parentNode != null ) {
                if(!root.arrangement.equals("bilateral")) {
                    deleteNode();
                }else {
                    deleteBothNode();
                }
                redoundo.operation(this,"delete");
                redoundo.stackClean();
            }
        });

        if (parentNode != null) {
            level = parentNode.getLevel() + 1;
        }

        //输入框切换
        label.setOnMouseClicked(this::exchangeTextArea);


        //文本框失去焦点
        textArea.focusedProperty().addListener((observableValue, isLose, isFocus) -> {
            if (!isFocus) {
                hideTextArea();
                label.setVisible(true);
                label.setDisable(false);
            }
        });


        //设置鼠标按下事件处理程序
        this.setOnMousePressed(event -> {
            x = event.getSceneX() - this.getLayoutX();
            y = event.getSceneY() - this.getLayoutY();
        });

        // 设置鼠标拖动事件处理程序
        this.setOnMouseDragged(this::moveNode);

    }

    public void mouse(){
        if ((root == null || root.equals(this)) && arrangement.equals("bilateral")) {
            addBothNode(null);
        } else if (parentNode != null) {
            if (root.arrangement.equals("bilateral")) {
                addBothNode(null);
            }
            else {
                addChrNode(null);
            }
        } else if(this.arrangement.equals("bilateral")){
            addBothNode(null);
        }else {
            addChrNode(null);
        }
        redoundo.operation(childrenNode.getLast(),"add");
        redoundo.stackClean();
        selectRect.setLayoutY(this.getLayoutY()-3);
    }

    public void initLocation(node n) {
        //用于设置节点位置
        if (n.parentNode != null) {
            if (n.arrangement.equals("right")) {
                n.setLayoutX(n.parentNode.getLayoutX() + n.parentNode.getPrefWidth() + 50);
                if (!n.parentNode.getChildrenNode().isEmpty()) {
                    n.setLayoutY(n.parentNode.minHeightNode.getLayoutY() + n.parentNode.getPrefHeight() + 10);
                } else {
                    //父节点没有子节点
                    n.setLayoutY(n.parentNode.getLayoutY());
                }
            } else if (n.arrangement.equals("left")) {
                n.setLayoutX(n.parentNode.getLayoutX() - n.parentNode.getPrefWidth() - 50);
                if (!n.parentNode.getChildrenNode().isEmpty()) {
                    //证明父节点有子节点
                    n.setLayoutY(n.parentNode.minHeightNode.getLayoutY() + n.parentNode.getPrefHeight() + 10);
                } else {
                    //父节点没有子节点
                    n.setLayoutY(n.parentNode.getLayoutY());
                }
            }
        } else {
            n.setLayoutX(100);
            n.setLayoutY(100);
        }
    }

    public void initLine() {
        if (parentNode != null) {
            line = new QuadCurve();
            if (this.arrangement.equals("right")) {
                line.startXProperty().bind(parentNode.layoutXProperty().add(getRectangle().getWidth()));
                line.endXProperty().bind(layoutXProperty());
            } else {
                line.startXProperty().bind(parentNode.layoutXProperty());
                line.endXProperty().bind(layoutXProperty().add(getRectangle().getWidth()));
            }
            line.startYProperty().bind(parentNode.layoutYProperty().add(getRectangle().getHeight() / 2));
            line.endYProperty().bind(layoutYProperty().add(getRectangle().getHeight() / 2));
            line.setFill(Color.TRANSPARENT);
            line.setStroke(Color.BLACK);
            line.setStrokeWidth(2);
            line.controlXProperty().bind(line.startXProperty());
            line.controlYProperty().bind(line.endYProperty());
            pane2.getChildren().add(line);

        }
    }


    //节点拖拽
    void moveNode(MouseEvent event) {
        if (event.getButton() == MouseButton.PRIMARY) {
            move(root, event.getSceneX() - x - this.getLayoutX(), event.getSceneY() - y - this.getLayoutY());

            selectRect.setLayoutX(event.getSceneX() - x - 2);
            selectRect.setLayoutY(event.getSceneY() - y - 3);
          //  root.xlayout = event.getSceneX() - x;
          //  root.ylayout = event.getSceneY() - y;
        }
    }

    void move(node n, double x, double y) {
        if (n != null && !n.getChildrenNode().isEmpty()) {
            for (node n1 : n.getChildrenNode())
                move(n1, x, y);
        }
        if (n != null) {
            n.setLayoutX(x + n.getLayoutX());
            n.setLayoutY(y + n.getLayoutY());
        }
    }

    //切换文本框
    void exchangeTextArea(MouseEvent event) {
        if (event.getClickCount() == 1) {
            isEdit = true;
            showSelectRect();
            layoutController.getInstance().unbindLabelAndTextField();;
            select();
        }
        if (event.getClickCount() == 2 && event.getButton() == MouseButton.PRIMARY) {
            showTextArea();
            label.setVisible(false);
            label.setDisable(true);
        }
    }

    //用于隐藏文本框
    public void hideTextArea() {
        textArea.setDisable(true);
        textArea.setVisible(false);
    }

    //用于显示文本框
    public void showTextArea() {
        textArea.setDisable(false);
        textArea.setVisible(true);
        textArea.requestFocus();
    }


    //让选中框显示
    public void showSelectRect() {
        selectRect.setVisible(true);
    }


    //添加兄弟节点
    public void addBroNode() {
        //表示的是根节点，不能添加兄弟节点，只能添加子节点
        if(!root.arrangement.equals("bilateral")) {
            if (parentNode == null) {
                addChrNode(null);
            } else {
                parentNode.addChrNode(null);
            }
        }else {
            if (parentNode == null) {
                addBothNode(null);
            } else {
                parentNode.addBothNode(null);
            }
        }
    }

    public void initBothLocation(node n) {
        if (n.arrangement.equals("right")) {
            n.setLayoutX(n.parentNode.getLayoutX() + n.parentNode.getPrefWidth() + 50);
            //首先判断是不是2级节点
            if (n.level == 2) {
                //有2级节点
                if (rightChildrenNode.size() >= 1) {
                    n.setLayoutY(rightMinHeight.getLayoutY() + getPrefHeight() + 10);
                } else {
                    n.setLayoutY(n.parentNode.getLayoutY());
                }
            } else {
                if (!n.parentNode.getChildrenNode().isEmpty()) {
                    n.setLayoutY(n.parentNode.minHeightNode.getLayoutY() + getPrefHeight() + 10);
                } else {
                    //父节点没有子节点
                    n.setLayoutY(n.parentNode.getLayoutY());
                }
            }
        } else {
            n.setLayoutX(n.parentNode.getLayoutX() - n.parentNode.getPrefWidth() - 50);
            //首先判断是不是2级节点
            if (n.level == 2) {
                //有2级节点
                if (leftChildrenNode.size() >= 1) {
                    n.setLayoutY(leftMinHeight.getLayoutY() + getPrefHeight() + 10);
                } else {
                    n.setLayoutY(n.parentNode.getLayoutY());
                }
            } else {
                if (!n.parentNode.getChildrenNode().isEmpty()) {
                    n.setLayoutY(n.parentNode.minHeightNode.getLayoutY() + getPrefHeight() + 10);
                } else {
                    //父节点没有子节点
                    n.setLayoutY(n.parentNode.getLayoutY());
                }
            }
        }
    }

    public void ConfirmBoth(node n) {
        if (root.arrangement.equals("bilateral")) {
            LinkedList<node> al = new LinkedList<>();
            sumNode(root, al);
            if (!al.isEmpty()) {
                for (node n1 : al) {
                    if (n1.arrangement.equals("right") && rightMaxHeight == null) {
                        rightMaxHeight = n1;
                    }
                    if (n1.arrangement.equals("right") && n1.getLayoutY() < rightMaxHeight.getLayoutY()) {
                        rightMaxHeight = n1;
                    }

                    if (n1.arrangement.equals("left") && leftMaxHeight == null) {
                        leftMaxHeight = n1;
                    }
                    if (n1.arrangement.equals("left") && n1.getLayoutY() < leftMaxHeight.getLayoutY()) {
                        leftMaxHeight = n1;
                    }
                }
                for (node n1 : al) {
                    if (n1.arrangement.equals("right") && rightMinHeight == null) {
                        rightMinHeight = n1;
                    }
                    if (n1.arrangement.equals("right") && n1.getLayoutY() > rightMinHeight.getLayoutY()) {
                        rightMinHeight = n1;
                    }

                    if (n1.arrangement.equals("left") && leftMinHeight == null) {
                        leftMinHeight = n1;
                    }
                    if (n1.arrangement.equals("left") && n1.getLayoutY() > leftMinHeight.getLayoutY()) {
                        leftMinHeight = n1;
                    }
                }
            }
        }

        LinkedList<node> cal = new LinkedList<>();
        sumNode(n, cal);
        if (!cal.isEmpty()) {
            node maxHeight = cal.get(0);
            node minHeight = cal.get(0);
            for (node n1 : cal) {
                if (n1.getLayoutY() < maxHeight.getLayoutY()) {
                    maxHeight = n1;
                }
            }
            for (node n1 : cal) {
                if (n1.getLayoutY() > minHeight.getLayoutY()) {
                    minHeight = n1;
                }
            }
            n.maxHeightNode = maxHeight;
            n.minHeightNode = minHeight;
        }else {
            n.maxHeightNode = null;
            n.minHeightNode = null;
        }

    }

    public void setPerSufNode(LinkedList<node> temp, node newNode) {
        if (temp.size() >= 1) {
            node n2;//用于记录子节点中哪个是高度最低的节点
            n2 = temp.get(0);
            for (node n0 : temp) {
                if (n0.getLayoutY() > n2.getLayoutY()) {
                    n2 = n0;
                }
            }
            n2.SufNode = newNode;
            newNode.PreNode = n2;
            minHeightNode = newNode;
        }
    }

    //若布局时双边布局时使用这个方法增加节点
    public void addBothNode(node n) {
        ConfirmBoth(this);
        node newNode = null;
        if(n != null) {
            newNode = n;
        }else {
            if(parentNode != null){
                newNode = new node(root, this, pane1, pane2, this.arrangement);
            }else if(rightChildrenNode.size() <= leftChildrenNode.size()){
                newNode = new node(root, this, pane1, pane2, "right");
            }else {
                newNode = new node(root, this, pane1, pane2, "left");
            }
        }
        if (parentNode != null) {
            initBothLocation(newNode);
            if (minHeightNode == null) {
                minHeightNode = newNode;
                maxHeightNode = newNode;
            } else {
                setPerSufNode(childrenNode, newNode);
            }
        } else {
            if (newNode.arrangement.equals("right")) {
                initBothLocation(newNode);
                if (rightMinHeight == null) {
                    rightMinHeight = newNode;
                    rightMaxHeight = newNode;
                } else {
                    setPerSufNode(rightChildrenNode, newNode);
                }
                rightChildrenNode.add(newNode);
            } else {
                initBothLocation(newNode);
                if (leftMinHeight == null) {
                    leftMinHeight = newNode;
                    leftMaxHeight = newNode;
                } else {
                    setPerSufNode(leftChildrenNode, newNode);
                }
                leftChildrenNode.add(newNode);
            }
        }
        // initBothLocation(newNode);
        childrenNode.add(newNode);
        if(newNode.parentNode != null){
            newNode.parentNode.treeItem.getChildren().add(newNode.treeItem);
        }
        if(!(newNode.getLayoutY() == newNode.parentNode.getLayoutY())) {
            ConfirmBoth(this);
            Calbro(this.SufNode);
            Calpar(this.parentNode);
            ConfirmBoth(this);
            double rh = 0, lh = 0;
            if (leftMaxHeight != null) {
                lh = leftMinHeight.getLayoutY() - leftMaxHeight.getLayoutY();
            }
            if (rightMinHeight != null) {
                rh = rightMinHeight.getLayoutY() - rightMaxHeight.getLayoutY();
            }
            if ((lh == 0 && newNode.arrangement.equals("left")) || (rh == 0 && newNode.arrangement.equals("right"))) {
            } else {
                LinkedList<node> cal = new LinkedList<>();
                sumNode(root, cal);
                if (lh > rh) {
                    if (newNode.arrangement.equals("right")) {
                        if(this.level >= 2) {
                            this.setLayoutY(this.getLayoutY() + 25);
                            root.setLayoutY(root.getLayoutY() - 25);
                        }

                        for (node n2 : cal) {
                            if (n2.arrangement.equals("right")) {
                                n2.setLayoutY(n2.getLayoutY() - 25);
                            }
                        }
                    } else {
                        if(this.level >= 2) {
                            this.setLayoutY(this.getLayoutY() + 25);
                        }
                        if(this.parentNode == null) {
                            root.setLayoutY(root.getLayoutY() + 25);
                        }
                        for (node n2 : cal) {
                            if (n2.arrangement.equals("right")) {
                                n2.setLayoutY(n2.getLayoutY() + 25);
                            }
                        }
                    }
                } else if (lh < rh) {
                    if (newNode.arrangement.equals("left")) {
                        if(this.level >= 2) {
                            this.setLayoutY(this.getLayoutY() + 25);
                            root.setLayoutY(root.getLayoutY() - 25);
                        }
                        for (node n2 : cal) {
                            if (n2.arrangement.equals("left")) {
                                n2.setLayoutY(n2.getLayoutY() - 25);
                            }
                        }
                    } else {
                        if(this.level >= 2) {
                            this.setLayoutY(this.getLayoutY() + 25);
                        }
                        if(this.parentNode == null) {
                            root.setLayoutY(root.getLayoutY() + 25);
                        }
                        for (node n2 : cal) {
                            if (n2.arrangement.equals("left")) {
                                n2.setLayoutY(n2.getLayoutY() + 25);
                            }
                        }
                    }
                } else {
                    if (newNode.arrangement.equals("right")) {
                        if(this.level >= 2) {
                            this.setLayoutY(this.getLayoutY() + 25);
                            root.setLayoutY(root.getLayoutY() - 25);
                        }
                        for (node n2 : cal) {
                            if (n2.arrangement.equals("right")) {
                                n2.setLayoutY(n2.getLayoutY() - 25);
                            }
                        }
                    } else {
                        if(this.level >= 2) {
                            this.setLayoutY(this.getLayoutY() + 25);
                            root.setLayoutY(root.getLayoutY() - 25);
                        }
                        for (node n2 : cal) {
                            if (n2.arrangement.equals("left")) {
                                n2.setLayoutY(n2.getLayoutY() - 25);
                            }
                        }
                    }
                }
            }
        }
    }

    //添加节点是用于计算要增加的高度
    public void Calson(node n) {
        System.out.println("1");
        if (!n.getChildrenNode().isEmpty()) {
            for (node n1 : n.getChildrenNode()) {
                Calson(n1);
            }
            n.setLayoutY(n.getLayoutY() + 50);
        } else {
            n.setLayoutY(n.getLayoutY() + 50);
        }
    }

    public void Calbro(node n) {
        if (n != null) {
            System.out.println("2");
            Calson(n);
            Calbro(n.SufNode);
        }
    }

    public void Calpar(node n) {
        if (n != null) {
            System.out.println("3");
            ConfirmBoth(n);
            n.setLayoutY(n.getLayoutY() + 25);
            Calbro(n.SufNode);
            Calpar(n.parentNode);
        }
    }

    //添加儿子节点
    public void addChrNode(node n) {
        if(this.arrangement.equals("bilateral") ||(root!=null && root.arrangement.equals("bilateral"))){
            addBothNode(n);
            return;
        }
        node newNode;
        ConfirmBoth(this);
        if (n != null) {//read可序列文件生成新节点
            newNode = n;
        } else {//右键添加新节点
            newNode = new node(root, this, pane1, pane2, this.arrangement);

        }
        initLocation(newNode);

        if (minHeightNode == null) {
            minHeightNode = newNode;
            maxHeightNode = newNode;
        } else {
            setPerSufNode(childrenNode, newNode);
        }

        childrenNode.add(newNode);
        ConfirmBoth(this);
        if(newNode.parentNode != null){
            newNode.parentNode.treeItem.getChildren().add(newNode.treeItem);
        }
        if (getChildrenNode().size() != 1) {
            this.setLayoutY((minHeightNode.getLayoutY() + maxHeightNode.getLayoutY()) / 2);
            Calbro(this.SufNode);
            Calpar(this.parentNode);

        }
    }
    //存储要删除的节点删除节点
    public void sumDeleteNode(node Node) {
        if (!Node.childrenNode.isEmpty()) {
            for (node n : Node.childrenNode) {
                n.sumDeleteNode(n);
                deleteList.add(n);
            }
        }
    }

    //存储节点
    public void sumNode(node Node, LinkedList<node> cal) {
        if (!Node.childrenNode.isEmpty()) {
            for (node n : Node.childrenNode) {
                n.sumNode(n, cal);
                cal.add(n);
            }
        }
    }


    public void Calupson(node n) {
        if (!n.getChildrenNode().isEmpty()) {
            for (node n1 : n.getChildrenNode()) {
                Calupson(n1);
            }
            n.setLayoutY((n.maxHeightNode.getLayoutY() + n.minHeightNode.getLayoutY()) / 2);
        } else {
            n.setLayoutY(n.getLayoutY() - distance - 50);
        }
    }

    public void Calupbro(node n) {
        if (n != null) {
            Calupson(n);
            Calupbro(n.SufNode);
        }
    }

    public void Caluppar(node n) {
        if (n != null) {
            ConfirmBoth(n);
            if (n.minHeightNode != null && n.maxHeightNode != null) {
                n.setLayoutY((n.maxHeightNode.getLayoutY() + n.minHeightNode.getLayoutY()) / 2);
            }
            Calupbro(n.SufNode);
            Caluppar(n.parentNode);
        }
    }

    //删除双边布局节点
    public void deleteBothNode() {
        ConfirmBoth(this);
        root.treeItem.getChildren().remove(treeItem);
        if (this.maxHeightNode != null && this.minHeightNode != null) {
            distance = this.minHeightNode.getLayoutY() - this.maxHeightNode.getLayoutY();
        } else {
            distance = 0;
        }
        deleteList.clear();
        sumDeleteNode(this);
        for (node n : deleteList) {
            n.parentNode.getTreeItem().getChildren().remove(n.treeItem);
            pane1.getChildren().remove(n);
            pane2.getChildren().remove(n.line);
            if(n.equals(rightMaxHeight)){
                rightMaxHeight = null;
            }
            if(n.equals(rightMinHeight)){
                rightMinHeight = null;
            }
            if(n.equals(leftMaxHeight)){
                leftMaxHeight = null;
            }
            if(n.equals(leftMinHeight)){
                leftMinHeight = null;
            }
        }
        if(parentNode != null) {
            this.parentNode.getTreeItem().getChildren().remove(this.treeItem);
        }
        pane1.getChildren().remove(this);
        pane2.getChildren().remove(this.line);
        if (parentNode != null) {
            parentNode.getChildrenNode().remove(this);
            if (parentNode.minHeightNode.equals(this)) {
                parentNode.minHeightNode = null;
            }
            if (parentNode.maxHeightNode.equals(this)) {
                parentNode.maxHeightNode = null;
            }
        }
        if(!parentNode.childrenNode.isEmpty()) {
            double temp1 = root.getLayoutY();
            Calupbro(this.SufNode);
            Caluppar(this.parentNode);
            root.setLayoutY(temp1);
            if(this.level == 2){
                if(this.arrangement.equals("right")){
                    rightChildrenNode.remove(this);
                }else {
                    leftChildrenNode.remove(this);
                }
            }
            if(rightMaxHeight!=null) {
                if (rightMaxHeight.equals(this)) {
                    rightMaxHeight = null;
                }
            }
            if(rightMinHeight!=null) {
                if (rightMinHeight.equals(this)) {
                    rightMinHeight = null;
                }
            }
            if(leftMaxHeight!=null) {
                if (leftMaxHeight.equals(this)) {
                    leftMaxHeight = null;
                }
            }
            if(leftMinHeight!=null) {
                if (leftMinHeight.equals(this)) {
                    leftMinHeight = null;
                }
            }

            ConfirmBoth(root);
            double temp3 ;
            LinkedList<node> cal = new LinkedList<>();
            sumNode(root,cal);
            if(this.arrangement.equals("right")){
                if(rightMinHeight != null && rightMaxHeight!=null) {
                    temp3 = (rightMinHeight.getLayoutY() + rightMaxHeight.getLayoutY()) / 2;
                    temp3 = temp1 - temp3;
                    for (node nt : cal) {
                        if (nt.arrangement.equals("right")) {
                            nt.setLayoutY(nt.getLayoutY() + temp3);
                        }
                    }
                }
            }else {
                if(leftMinHeight!=null&&leftMaxHeight!=null) {
                    temp3 = (leftMinHeight.getLayoutY() + leftMaxHeight.getLayoutY()) / 2;
                    temp3 = temp1 - temp3;
                    for (node nt : cal) {
                        if (nt.arrangement.equals("left")) {
                            nt.setLayoutY(nt.getLayoutY() + temp3);
                        }
                    }
                }
            }

        }
        if (this.PreNode != null) {
            this.PreNode.SufNode = this.SufNode;
        }
        if (this.SufNode != null) {
            this.SufNode.PreNode = this.PreNode;
        }
        this.SufNode = null;
        this.PreNode = null;
        getInstance().hideSelectRect();
        getInstance().selectNode = null;

    }


    //删除节点
    public void deleteNode() {
        ConfirmBoth(this);
        if (this.maxHeightNode != null && this.minHeightNode != null) {
            distance = this.minHeightNode.getLayoutY() - this.maxHeightNode.getLayoutY();
        } else {
            distance = 0;
        }
        deleteList.clear();
        sumDeleteNode(this);
        for (node n : deleteList) {
            pane1.getChildren().remove(n);
            pane2.getChildren().remove(n.line);
            n.parentNode.getTreeItem().getChildren().remove(n.treeItem);
        }
        if(parentNode!=null) {
            this.parentNode.getTreeItem().getChildren().remove(treeItem);
        }
        pane1.getChildren().remove(this);
        pane2.getChildren().remove(this.line);
        if (parentNode != null) {
            parentNode.getChildrenNode().remove(this);
            if (parentNode.minHeightNode.equals(this)) {
                parentNode.minHeightNode = null;
            }
            if (parentNode.maxHeightNode.equals(this)) {
                parentNode.maxHeightNode = null;
            }
        }
        if( parentNode!= null && !parentNode.getChildrenNode().isEmpty()) {
            Calupbro(this.SufNode);
            Caluppar(this.parentNode);
        }
        if (this.PreNode != null) {
            this.PreNode.SufNode = this.SufNode;
        }
        if (this.SufNode != null) {
            this.SufNode.PreNode = this.PreNode;
        }
        this.SufNode = null;
        this.PreNode = null;
        getInstance().hideSelectRect();
        getInstance().selectNode = null;
    }


    //当节点被选中时调用
    public void select() {
        isEdit = true;
        getInstance().selectNode = null;
        getInstance().selectNode(this);

    }


    //设置边框颜色
    public void setBorderColor(Color color) {
        borderColor = color;
        rectangle.setStroke(borderColor);
    }

    //设置边框弧度
    public void setRadius(double radius) {
        rectangle.setArcWidth(radius);
        rectangle.setArcHeight(radius);
    }

    //设置节点宽度
    public void setWidth(double width) {
        selectRect.setWidth(width);
    }

    //设置节点高度
    public void setHeight(double height) {
        selectRect.setHeight(height);
    }

    //设置节点X
    public void setX(double X) {
        rectangle.setLayoutX(X);
    //   selectRect.setLayoutX(X);
    //    getInstance().selectNode.textArea.setTranslateX(X);
    }
    //设置节点Y
    public void setY(double Y) {
        rectangle.setLayoutY(Y);
   //     selectRect.setLayoutY(Y);
   //     getInstance().selectNode.textArea.setTranslateY(Y);

    }
    //张世杰 将颜色转换为16进制表示
    private String toHex(Color color) {
        String hex = String.format("#%02X%02X%02X", (int) (color.getRed() * 255), (int) (color.getGreen() * 255), (int) (color.getBlue() * 255));
        return hex;
    }

    //设置填充颜色
    public void setRectangleColor(Color color) {
        rectangleColor = color;
        rectangle.setFill(rectangleColor);
    }


    //更改字体颜色
    public void ChangeFontColor(boolean isEdit) {
        Color Textcolor = getInstance().getTextColor();
        getInstance().selectNode.label.setTextFill(Textcolor);
    }


    //更改填充颜色
    public void ChangeFillColor(boolean isEdit) {
        Color Fillcolor = getInstance().getFillColor();
//        layoutController.getInstance().selectNode.rectangle.setFill(Fillcolor);
        setRectangleColor(Fillcolor);
    }

    //更改画布颜色
    public void ChangeBackgroundColor() {
        Color BackgroundColor = getInstance().getBackgroundColor();
        getInstance().setBackgroundColor(BackgroundColor);
    }

    //更改边框颜色
    public void ChangeBorderColor(boolean isEdit) {
        Color BorderColor = getInstance().getborderColor();
        setBorderColor(BorderColor);
    }
    //更改加粗
    public void ChangeBold(boolean isEdit) {
        boolean isBold = getInstance().getBold();
        getInstance().ChangeTextWeight(isBold);
        setBold(isBold);
    }
    //更改下划线
    public void ChangeUnderline(boolean isEdit) {
        boolean isUnderline = getInstance().getUnderline();
        getInstance().ChangeTextUnderline(isUnderline);
        setUnderline(isUnderline);
    }
    //更改边框弧度
    public void ChangeRadius(boolean isEdit) {
        double radius = getInstance().getRadius();
        setRadius(radius);
    }
    //更改节点高度
    public void ChangeHeight(boolean isEdit) {
        double height = getInstance().getHeightValue();
        setHeight(height);
    }
    //更改节点宽度
    public void ChangeWidth(boolean isEdit) {
        double width = getInstance().getWidthValue();
        setWidth(width);
    }
    //更改节点x
    public void ChangeX(boolean isEdit) {
        setX(xlayout);
    }
    //更改节点y
    public void ChangeY(boolean isEdit) {
        setY(ylayout);
    }
    public Rectangle getRectangle() {
        return this.rectangle;
    }

    public boolean getisEdit() {
        return isEdit;
    }


    public int getLevel() {
        return level;
    }

    public QuadCurve getLine() {
        return this.line;
    }

    public void setLevel(int level) {
        this.level = level;
    }


    public Label getLabel() {
        return label;
    }

    public void setLabel(Label label) {
        this.label = label;
    }

    public LinkedList<node> getChildrenNode() {
        return this.childrenNode;
    }

    public void addChildrenNode(node node) {
        this.childrenNode.add(node);
    }


    public node getParentNode() {
        return parentNode;
    }

    public void setParentNode(node parentNode) {
        this.parentNode = parentNode;
    }

    public void setRoot(node root) {
        this.root = root;
    }

    public Group getPane1() {
        return pane1;
    }

    public Group getPane2() {
        return pane2;
    }

    public void setBold(boolean bold) {
        isBold = bold;
    }
    public boolean getBold() {
        return isBold;
    }
    public void setUnderline(boolean underline) {
        isUnderline = underline;
    }
    public boolean getUnderline() {
        return isUnderline;
    }

/*    public Rectangle getrectangle()
    {
        return rectangle;
    }*/
    //读取矩形的各项属性
    public void CheckNode() {
        //layoutController.getInstance().setwhile();

      // 获取边框颜色
        Color borderColor1 = (Color) rectangle.getStroke();

      // 获取填充颜色
        Color fillColor2 = (Color) rectangle.getFill();

        layoutController.getInstance().setborderColor1(borderColor1);
        layoutController.getInstance().setfillColor1(fillColor2);
    }
}
